package com.learn.config;

import com.learn.springmvc4.interceptor.DemoInterceptor;
import com.learn.springmvc4.messageconverter.MyMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import java.util.List;

/**
 * @EnableWebMvc开启SpringMVC支持，若无词句，重写WebMvcConfigurerAdapter方法无效。
 * 继承WebMvcConfigurerAdapter类，重写其方法可对springmvc进行配置
 * addResourceLocations指的是文件放置的目录，addResourceHandler指的是对外暴露的访问路径
 * Created by xuxiaobao on 2017/8/3.
 */
@Configuration
@EnableWebMvc
@ComponentScan("com.learn.springmvc4")
public class MyMvcConfig extends WebMvcConfigurerAdapter{
    /*
    配置视图解析
     */
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver =
                new InternalResourceViewResolver();

        viewResolver.setPrefix("/WEB-INF/classes/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }
    /*
    配置文件上传
     */
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver =
                new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(100000);
        return multipartResolver;
    }
    //定义静态资源访问路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/asserts/");
    }
    //定义自定义拦截器
    public DemoInterceptor demoInterceptor() {
        return new DemoInterceptor();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(demoInterceptor());
    }

    @Override//简化页面直接跳转的配置
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("/index");
    }

    /*
    springmvc 中，路径参数如果带"."的话后面的值将被忽略
    通过配置configurePathMatch方法可不忽略"."后面的参数
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseRegisteredSuffixPatternMatch(false);
    }

    @Bean
    public MyMessageConverter converter() {
        return new MyMessageConverter();
    }
    /*
    重载这个方法会覆盖掉Spring MVC 里默认注册的多个HttpMessageConverter
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
    }

    /*
    重载此方法仅添加一个自定义的HttpMessageConverter,不覆盖默认的HttpMessageConverter
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //添加MessageConverter
        converters.add(converter());
    }
}
